#include "MainMenuState.h"
#include "VictoryState.h"
#include "TileSystem.h"

#include "stdafx.h"

#define EXIT_OPTION 5


/**************************************************************/
// GetInstance
//	- store the ONLY instance in global memory
//	- return the only instance by pointer
/*static*/ MainMenuState* MainMenuState::GetInstance(void)
{
	static MainMenuState s_Instance;

	return &s_Instance;
}


/**************************************************************/
// Enter
//	- reset the game & load resources
/*virtual*/ void MainMenuState::Enter(void)
{
	// Set background color
	//	SGD::GraphicsManager::GetInstance()->SetClearColor({ 50, 50, 50 });	// dark gray

	//m_hBackground = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/Graphics/Main_menu_background.jpg");

	if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(theGame->GetSong(theGame->MENU)))
		SGD::AudioManager::GetInstance()->PlayAudio(theGame->GetSong(theGame->MENU));
}

/**************************************************************/
// Exit
//	- deallocate / unload resources
/*virtual*/ void MainMenuState::Exit(void)
{
	//SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hBackground);
}


/**************************************************************/
// Input
//	- handle user input
/*virtual*/ bool MainMenuState::Input(void)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	//CONTROLLER INPUT MAPPING
	//0 = A
	//1 = B
	//2 = X
	//3 = Y
	//4 = left Bumper
	// Press Escape to jump to the exit option
	if (Game::GetInstance()->MenuBackInput())
		m_nCursor = EXIT_OPTION; //Set the cursor to exit


	// Move cursor
	if (Game::GetInstance()->MenuDownInput())
	{
		++m_nCursor;

		// Wrap around
		if (m_nCursor > EXIT_OPTION)		// should enumerate the options
			m_nCursor = 0;
	}
	else if (Game::GetInstance()->MenuUpInput())
	{
		--m_nCursor;

		// Wrap around
		if (m_nCursor < 0)
			m_nCursor = EXIT_OPTION;
	}


	// Select an option?
	if (Game::GetInstance()->MenuConfirmInput())
	{
		// Which option?
		if (m_nCursor == 0)		// PLAY
		{
			pAudio->PlayAudio(theGame->GetMenuConfirm());
			Game::GetInstance()->ChangeState(LoadGameState::GetInstance());
			return true;
		}
		// Which option?
		if (m_nCursor == 1)	//OPTIONS
		{
			pAudio->PlayAudio(theGame->GetMenuConfirm());
			Game::GetInstance()->ChangeState(OptionsState::GetInstance());
			return true;

		}
		if (m_nCursor == 2)//HOW TO PLAY
		{
			pAudio->PlayAudio(theGame->GetMenuConfirm());
			Game::GetInstance()->ChangeState(InstructionsState::GetInstance());
			return true;
		}
		if (m_nCursor == 3)//HIGH SCORES
		{
			pAudio->PlayAudio(theGame->GetMenuConfirm());
			Game::GetInstance()->ChangeState(HighScoreState::GetInstance());
			return true;
		}
		if (m_nCursor == 4)//CREDITS
		{
			pAudio->PlayAudio(theGame->GetMenuConfirm());
			Game::GetInstance()->ChangeState(CreditsState::GetInstance());

			//This is for cimplicity when testing what the victory state looks like
			//Game::GetInstance()->ChangeState(VictoryState::GetInstance());
			return true;
		}
		else if (m_nCursor == EXIT_OPTION)	// EXIT
		{
			pAudio->PlayAudio(theGame->GetMenuConfirm());
			return false;	// quit the game
		}
	}

	return true;	// keep playing
}


/**************************************************************/
// Update
//	- update Objects / animations
/*virtual*/ void MainMenuState::Update(float elapsedTime)
{
}


/**************************************************************/
// Render
//	- render Objects / menu options
/*virtual*/ void MainMenuState::Render(void)
{
	SGD::GraphicsManager::GetInstance()->DrawTexture(theGame->GetMenuBackground(), { 0, 0 }, 0, {}, {}, { 1, 1 });

	// Use the game's font
	BitmapFont* pFont = Game::GetInstance()->GetFont();

	// Align text based on window width
	int width = (int)Game::GetInstance()->GetScreenWidth();


	// Display the game title centered at 2x scale
	pFont->Draw("MEGA\n LANDS", (int)((width - (4 * 32 * 4.0f)) / 2), 50, 2.0f, {});

	// Display the menu options centered at 1x scale
	pFont->Draw("Play", (width - (4 * 32)) / 2, 300, 0.7f, { 255, 0, 0 });
	pFont->Draw("Options", (width - (4 * 32)) / 2, 350, 0.7f, { 255, 0, 0 });
	pFont->Draw("How to Play", (width - (4 * 32)) / 2, 400, 0.7f, { 255, 0, 0 });
	pFont->Draw("HIGH SCORES", (width - (4 * 32)) / 2, 450, 0.7f, { 255, 0, 0 });
	pFont->Draw("Credits", (width - (4 * 32)) / 2, 500, 0.7f, { 255, 0, 0 });
	pFont->Draw("Exit", (width - (4 * 32)) / 2, 550, 0.7f, { 255, 0, 0 });

	// Display the cursor next to the option
	SGD::GraphicsManager::GetInstance()->DrawTextureSection(theGame->GetCursorImage(), { ((width - (7 * 32)) / 2.0f) - 60, 320 + 47.0f * m_nCursor }, { 0, 0, 100, 35 });
}
